package com.qiku.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MP的字段自动填充
 */
@Component
@Slf4j
public class MyMateObjectHandler implements MetaObjectHandler {

    // 添加操作时会自动执行
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("updateTime" , LocalDateTime.now());
        metaObject.setValue("updateUser" , BaseContext.getCurrentId());
        metaObject.setValue("createTime" , LocalDateTime.now());
        metaObject.setValue("createUser" , BaseContext.getCurrentId());
    }

    //更新操作时自动执行
    @Override
    public void updateFill(MetaObject metaObject) {
        // 输出当前线程的ID
        log.info("当前线程的Id=" + Thread.currentThread().getId());
        // metaObject 表示原始数据
        metaObject.setValue("updateTime" , LocalDateTime.now());
        metaObject.setValue("updateUser" , BaseContext.getCurrentId());
    }
}
